import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router/index'

const instance = axios.create({
	baseURL: process.env.BASE_API, // api的base_url
	timeout: 20000	
})
instance.defaults.headers.post['Content-Type'] = 'application/json'
instance.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'

// request拦截器
instance.interceptors.request.use(config => {

	const userToken = JSON.parse(sessionStorage.getItem('token'))
	if (userToken) {
		config.headers['Token'] = "Jiaying-"+userToken
	}
	return config
}, error => {
	console.log(error)
	Promise.reject(error)
})
instance.interceptors.response.use(
	response => {
		return response
	},
	error => {
		console.log('err' + error)
		Message({
			message: error.msg,
			type: 'error',
			duration: 3 * 1000
		})
		return Promise.reject(error)
	}
)
const API = {
	get (url, params, callback) {
		return new Promise((resolve, reject) => {
			instance.get(url, {
				params: params,
				tm: +new Date()
			}).then((response) => {
				if (response.data.code === 200) {
					callback && callback(response.data.data)
					resolve(response.data.data)
				} else if (response.data.code === 0) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					reject(response.data.msg)
				} else if (response.data.code > 210 && response.data.code <= 220) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					callback && callback(response.data.data)
					resolve(response.data.data)
				} else if (response.data.code > 220 && response.data.code < 300) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
				} else if (response.data.code === 1) {
					//console.log(response)
					debugger
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					// 登出
					store.dispatch('logout').then(() => {
						router.push({
							path: '/login'
						})
					})
				} else if (response.data.code === 400) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					reject(response.data.msg)
				} else if (response.data.code === 403) {
					//console.log(response)
					Message({
						message: '没有访问权限，请联系管理员',
						type: 'error',
						duration: 3 * 1000
					})
					// 退回上一页
					router.go(-1)

				} else {
					console.log('服务器错误:' + response.data.msg)
					reject(response.data.msg)
				}
			}).catch((error) => {
				console.log('服务器错误!' + error)
				reject(error)
			})
		})
	},
	post (url, params, callback) {
		return new Promise((resolve, reject) => {
			instance.post(url, params).then((response) => {
				if (response.data.code === 200) {
					callback && callback(response.data.data)
					resolve(response.data.data)
				} else if (response.data.code === 0) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					reject(response.data.msg)
				} else if (response.data.code > 210 && response.data.code <= 220) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					callback && callback(response.data.data)
					resolve(response.data.data)
				} else if (response.data.code > 220 && response.data.code < 300) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
				} else if (response.data.code === 1) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					// 登出
					store.dispatch('logout').then(() => {
						router.push({
							path: '/login'
						})
					})
				} else if (response.data.code === 400) {
					//console.log(response)
					Message({
						message: response.data.msg,
						type: 'error',
						duration: 3 * 1000
					})
					reject(response.data.msg)

				} else if (response.data.code === 403) {
					//console.log(response)
					Message({
						message: '没有访问权限，请联系管理员',
						type: 'error',
						duration: 3 * 1000
					})
					// 退回上一页
					router.back(-1)

				} else {
					console.log('服务器错误:' + response.data.msg)
					reject(response.data.msg)
				}
			}).catch((error) => {
				console.log('服务器错误!' + error)
				reject(error)
			})
		})
	}
}
export { API }
